/* @(#)GSMarketInfoProcessor.java
 * 
 *  Project:GS
 *  
 *  Modify  Information:
 *  ===============================================
 *   Author          Date            Description
 *  -------------- ---------------  ---------------
 *  Jenny  Li      2006-5-26          新增
 * Copyright Notice:
 * =============================================================================
 *    Copyright (c) 2001-2006 Beijing HiTRUST Technology Co., Ltd. 1808 Room, 
 *        Science & Technology Building, No. 9 South Street, Zhong Guan Cun, 
 *        Haidian District, Beijing ,100081, China All rights reserved.
 *
 *    This software is the confidential and proprietary information of
 *        Beijing HiTRUST Technology Co., Ltd. ("Confidential Information").
 *        You shall not disclose such Confidential Information and shall use it
 *        only in accordance with the terms of the license agreement you entered
 *        into with HiTRUST.
 *
 * Warning:
 * =============================================================================
 *
 */
package com.hitrust.gs.business;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;

import org.apache.struts.upload.FormFile;

import com.hitrust.gs.bean.GSMarketInfoBean;
import com.hitrust.gs.bean.GSOperatorBean;
import com.hitrust.gs.dao.GSMarketInfoDAO;
import com.hitrust.gs.form.GSMarketInfoAddForm;
import com.hitrust.gs.form.GSMarketInfoModForm;
import com.hitrust.gs.lib.GSBaseConstants;
import com.hitrust.gs.lib.GSLogWriter;
import com.hitrust.gs.lib.GSSystemConfigurator;
import com.hitrust.gs.service.GSBaseProcesser;
import com.hitrust.gs.util.PubTool;
import com.hitrust.gs.util.exception.GSException;
import com.hitrust.gs.vo.GSMarketInfoQryVO;

/**
 * @author JennyLi
 *
 */
public class GSMarketInfoProcessor implements GSBaseProcesser{
    
    /**
     * 查询市场信息
     * @param aVO               查询参数
     * @param pageNo            页号
     * @param pageSize          每页笔数
     * @return
     * @throws GSException
     */
    public ArrayList getMarketInfoByOpt(GSMarketInfoQryVO aVO,int pageNo,int pageSize) throws GSException{
        ArrayList tList = new ArrayList();
        try{
            GSMarketInfoDAO tMarketInfoDAO = new GSMarketInfoDAO();
            tList = tMarketInfoDAO.queryMarketInfoByOpt(aVO,pageNo,pageSize);
        }catch(GSException ge){
            throw ge;
        }
        return tList;
    }
    
    /**
     * 查询市场信息
     * @return
     * @throws GSException
     */
    public ArrayList getMarketInfoAll() throws GSException{
        ArrayList tList = new ArrayList();
        try{
            GSMarketInfoDAO tMarketInfoDAO = new GSMarketInfoDAO();
            tList = tMarketInfoDAO.queryMarketInfoAll();
        }catch(GSException ge){
            throw ge;
        }
        return tList;
    }
    
    /**
     * 新增市场信息
     * @param aForm             页面提交的参数
     * @return
     * @throws GSException
     */
    public boolean addMarketInfo(GSMarketInfoAddForm aForm) throws GSException{
        boolean tFlag = false;
        GSMarketInfoDAO tMarketInfoDAO = new GSMarketInfoDAO();
        try{
            GSMarketInfoBean tMarketInfoBean = new GSMarketInfoBean();
            tMarketInfoBean.setGs_info_title(aForm.getNewTitle());
            tMarketInfoBean.setGs_info_content(aForm.getNewContent());
            tMarketInfoBean.setGs_provider_id(aForm.getNewProvider());
            tMarketInfoBean.setGs_publish_date(aForm.getNewPublishDate());
            String tFileName = uploadFile(aForm.getFileName());
            tMarketInfoBean.setGs_file_name(tFileName);
            GSOperatorBean tOperBean = (GSOperatorBean)aForm.getSession().getAttribute(GSBaseConstants.LOGIN);
            tMarketInfoBean.setGs_create_operater(tOperBean.getGs_oper_id());
            tMarketInfoBean.setGs_lastmodifer_id(tOperBean.getGs_oper_id());
            tFlag = tMarketInfoDAO.insertMarketInfo(tMarketInfoBean);
        }catch(GSException ge){
            throw ge;
        }catch(Exception e){
            GSLogWriter.error(this,e.toString());
            throw new GSException(e.toString());
        }
        return tFlag;
    }
    /**
     * 修改市场信息
     * @param aForm             页面提交的参数  
     * @return
     * @throws GSException
     */
    public boolean modMarketInfo(GSMarketInfoModForm aForm) throws GSException{
        boolean tFlag = false;
        GSMarketInfoDAO tMarketInfoDAO = new GSMarketInfoDAO();
        try{
            GSMarketInfoBean tMarketInfoBean = new GSMarketInfoBean();
            tMarketInfoBean.setGs_info_title(aForm.getNewTitle());
            tMarketInfoBean.setGs_info_content(aForm.getNewContent());
            tMarketInfoBean.setGs_provider_id(aForm.getNewProvider());
            tMarketInfoBean.setGs_publish_date(aForm.getNewPublishDate());
            tMarketInfoBean.setGs_create_date(aForm.getCreateDate());
            //上传附件
            String tFileName = uploadFile(aForm.getFileName());
            //上传成功返回文件名称存入DB，失败或无新文件上传则保存旧文件名称
            if("".equals(tFileName)){
                tMarketInfoBean.setGs_file_name(aForm.getOldFileName());
            }else
                tMarketInfoBean.setGs_file_name(tFileName);
            GSOperatorBean tOperBean = (GSOperatorBean)aForm.getSession().getAttribute(GSBaseConstants.LOGIN);
            tMarketInfoBean.setGs_lastmodifer_id(tOperBean.getGs_oper_id());
            tFlag = tMarketInfoDAO.updateMarketInfo(tMarketInfoBean);
        }catch(GSException ge){
            throw ge;
        }catch(Exception e){
            GSLogWriter.error(this,e.toString());
            throw new GSException(e.toString());
        }
        return tFlag;
    }    
    /**
     * 修改上传的附件信息
     * @param aForm             页面参数
     * @return
     * @throws GSException
     */
    public boolean modMarketInfoAndDelOldFile(GSMarketInfoModForm aForm) throws GSException{
        boolean tFlag = false;
        GSMarketInfoDAO tMarketInfoDAO = new GSMarketInfoDAO();
        try{
            GSMarketInfoBean tMarketInfoBean = new GSMarketInfoBean();
            tMarketInfoBean.setGs_info_title(aForm.getNewTitle());
            tMarketInfoBean.setGs_create_date(aForm.getCreateDate());
            //取得保存路径
            String tFilePath = GSSystemConfigurator.getParam("gs.upload.market.savepath");
            File tFile = new File(tFilePath + File.separator + aForm.getOldFileName());
            if(tFile.exists() && tFile.isFile()){
                boolean tFlag2 = tFile.delete();
                if(tFlag2)
                    GSLogWriter.info(this,"原文件删除成功!");
                else{
                    GSLogWriter.error(this,"原文件删除失败!");
                    throw new GSException("原文件删除失败!");
                }
            }else{
                GSLogWriter.error(this,"原文件不存在!");
                throw new GSException("原文件不存在!");
            }
            //上传新文件并返回文件名称
            String tFileName = uploadFile(aForm.getFileName());
            tMarketInfoBean.setGs_file_name(tFileName);
            //取得当前系统操作员编码
            GSOperatorBean tOperBean = (GSOperatorBean)aForm.getSession().getAttribute(GSBaseConstants.LOGIN);
            tMarketInfoBean.setGs_lastmodifer_id(tOperBean.getGs_oper_id());
            //修改市场信息表
            tFlag = tMarketInfoDAO.updateMarketInfoFile(tMarketInfoBean);
        }catch(GSException ge){
            throw ge;
        }catch(Exception e){
            GSLogWriter.error(this,e.toString());
            throw new GSException(e.toString());
        }
        return tFlag;
    }    
    
    /**
     * 文件上传
     * 
     * @param file
     * @return          文件名称
     * @throws Exception
     */
    private String uploadFile(FormFile aFile) throws IOException,GSException {
        String tFileName = "";
        if (aFile.getFileSize() == 0) {
            GSLogWriter.info("文件大小为0字节！");
            //throw new GSException("上传文件大小不能为0字节");
            return tFileName;
        }
        String savePath = GSSystemConfigurator.getParam("gs.upload.market.savepath");
        savePath = PubTool.apendSepa(savePath);
        tFileName = aFile.getFileName();
        if (tFileName.length() > 200) {
            GSLogWriter.error("文件名称不能大于200个字符!");
            throw new GSException("文件名称不能大于200个字符!");
        }
        InputStream stream = aFile.getInputStream();// 把文件传入
        String buf = Long.toString(System.currentTimeMillis()/100);        
        String tNewFileName=tFileName.substring(0,tFileName.indexOf(".")) + buf;
        String tFileType=tFileName.substring(tFileName.indexOf("."));
        tNewFileName = tNewFileName + tFileType;
        OutputStream bos = new FileOutputStream(savePath + tNewFileName);// 建立上传文件的输出流
        int bytesRead = 0;
        byte[] buffer = new byte[8192];
        while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
            bos.write(buffer, 0, bytesRead);// 文件传入服务器
        }
        bos.close();
        stream.close();
        return tNewFileName;
    }
    /**
     * 根据创建时间查询市场信息
     * @param aCreateDate           创建时间
     * @return
     * @throws GSException
     */
    public GSMarketInfoQryVO getMarketInfoByCD(String aCreateDate) throws GSException{
        GSMarketInfoQryVO tVO = null;
        GSMarketInfoDAO tDAO = new GSMarketInfoDAO();
        ArrayList tList = tDAO.queryMarketInfoByCD(aCreateDate);
        if(tList.size() == 1){
            tVO = (GSMarketInfoQryVO)tList.get(0);
        }else{
            throw new GSException("未查询到市场信息明细，请检查数据！");
        }
        return tVO;
    }
    /**
     * 删除市场信息
     * @param aCreateDate           创建时间
     * @return
     * @throws GSException
     */
    public boolean delMarketInfo(String aCreateDate) throws GSException{
        boolean tFlag = false; 
        GSMarketInfoDAO tDAO = new GSMarketInfoDAO();
        tFlag = tDAO.deleteMarketInfo(aCreateDate);
        return tFlag;
    }
}
